package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.pojo.R;
import com.pojo.Returner;
import com.service.ReturnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class ReturnerController {
    @Autowired
    private ReturnerService returnerService;

    /**
     * 根据项目id来查询回报信息
     * @param projectID
     * @return
     */
    @RequestMapping("selectAllReturnByProjectId")
    public Object selectAllReturnByProjectId(long projectID){
        List<Returner> returners = returnerService.selectReturnByprojectid(projectID);
        return returners;
    }

    /**
     * 添加回报信息
     * @param request
     * @return
     */
    @RequestMapping("/insertReturner")
    public Object insertReturner(HttpServletRequest request){
        String projectID = request.getParameter("projectID");
        String returner = request.getParameter("returner");
        Returner returner1 = JSONArray.parseObject(returner, Returner.class);
        returner1.setProjectid(Long.parseLong(projectID));
        int i = returnerService.insertReturner(returner1);
        return i;
    }

    @RequestMapping("/selectReturnerByReturnid")
    public Object selectByreturnid(long id){
        return returnerService.selectByreturnid(id);
    }
    @RequestMapping("updateReturnerCount")
    public Object updateReturnerCount(long id,long count){
        int i = returnerService.updateReturnerCount(id, count);
        R r=new R<>();
        if(i!=0){
            r.setCode("200");
        }else {
            r.setCode("400");
        }
        return r;
    }

    /**
     * 根据项目id来发放项目回报
     * @param projectId
     * @return
     */
    @RequestMapping("/fafang")
    public Object fafang(long projectId){
        int fangfa = returnerService.fangfa(projectId);
        R r=new R<>();
        if(fangfa!=0){
            r.setCode("200");
            r.setMess("回报已发放");
        }else {
            r.setCode("400");
        }
        return r;
    }
}
